package com.hlg.xboot.model;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>Description: 字典表树结构基本类型</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: 智悦信息</p>
 * <p>Site: www.zhiyuesoft.com</p>
 * @author TZW
 * @version 1.0
 * @date 2017/07/31
 */
@MappedSuperclass
public class BaseDicTree extends BaseDic {

    @Column(name = "parent_code")
    public String parentCode;

    @Transient
    public Set<BaseDicTree> children = new LinkedHashSet<>();

    public String getParentCode() {
        return parentCode;
    }

    public void setParentCode(String parentCode) {
        this.parentCode = parentCode;
    }

    public Set<BaseDicTree> getChildren() {
        return children;
    }

    public void setChildren(Set<BaseDicTree> children) {
        this.children = children;
    }

    public Map<String, Object> transformTreeModel(){
        Map<String, Object> data = super.transformTreeModel();
        data.put("parentCode", getParentCode());
        if(children.size() > 0) {
            data.put("children", children.stream().map((item) -> item.transformTreeModel()).collect(Collectors.toList()));
        }else{
            data.put("isLeaf", true);
        }
        return data;
    }
}
